<template>
  <a-modal
    title="新增生产计划"
    width="80%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <proplan-table-form-item
          :dataSource="workOrderList"
          :proList="list.proList"
          :plaNum="form.getFieldValue('plaNum') ? form.getFieldValue('plaNum') : null"
          @add="
            ({ tableData }) => {
              workOrderList = tableData
            }
          "
          @del="
            ({ tableData }) => {
              workOrderList = tableData
            }
          "
        />
        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-textarea
            :rows="3"
            placeholder="请输入备注信息"
            v-decorator="['remarks']"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { proPlanAdd } from '@/api/modular/main/proplan/proPlanManage'
import proplanTableFormItem from './proplanTableFormItem.vue'
import { proList } from '@/api/modular/main/pro/proManage'
export default {
  components: { proplanTableFormItem },
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 20 }
      },
      labelCol_T: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol_T: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      list: {
        // 产品
        proList: []
      },
      // 生产订单
      workOrderList: [],
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  provide() {
    return {
      getProList: this.getProList
    }
  },
  methods: {
    // moment,
    // 初始化方法
    async add(record) {
      this.time = new Date().toLocaleDateString()
      await this.getProList()
      this.visible = true
      const inTypeOption = this.$options
      this.inTypeData = inTypeOption.filters['dictData']('in_type')
      if (!record) {
        return
      }
      if (record.saleOrderList && record.saleOrderList.length > 0) {
        this.saleOrderList = record.saleOrderList.map((item) => {
          item.id = undefined
          return item
        })
      }
    },
    // 获取prolist表单
    async getProList() {
      this.list.proList = []
      var promise = proList().then((res) => {
        this.list.proList = res.data
      })
      await promise
      return this.list.proList
    },
    /**
     * 提交表单
     */
    handleSubmit() {
      const {
        form: { validateFields }
      } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          for (const key in values) {
            if (typeof values[key] === 'object' && values[key] != null) {
              values[key] = JSON.stringify(values[key])
            }
          }
          // 过滤空值
          if (this.workOrderList.length === 0) {
            this.confirmLoading = false
            return this.$message.error('产品不能为空')
          }
          // 传数据
          values.workOrderList = this.workOrderList || null
          // 去除编号空格
          values.code = values.code ? values.code.trim() : values.code
          proPlanAdd(values)
            .then((res) => {
              if (res.success) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error(res.message) // + res.message
              }
            })
            .finally((res) => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
      this.workOrderList = []
      this.form.resetFields()
    }
  }
}
</script>
